/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-5-22 下午12:38 Created by dwzhang
 */
package com.jade.bss.api.controller;

import com.jade.bss.api.Constants;
//import com.jade.bss.api.pdu.ForgetPasswordRequest;
//import com.jade.bss.api.pdu.ModifyPasswordRequest;
//import com.jade.bss.api.pdu.RegisterRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 客户控制器。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-5-22 下午12:38
 */
@Controller
@RequestMapping (value = "/customer")
public class CustomerController
{
//    protected Journal journal = JournalUtils.getJournal(LoginJournal.NAME);
//
//    private CustomerManager customerManager = ApplicationContextUtils.getBean("bss_customerManager");
//    private ProvisionService provisionService = ApplicationContextUtils.getBean("bss_provisionService");
//    private VerifyCodeManager verifyCodeManager = ApplicationContextUtils.getBean("bss_verifyCodeManager");
//    private Log log = LogFactory.getLog(CustomerController.class);
//
//    @RequestMapping (value = "/sendVerifyCode.json")
//    public void sendVerifyCode(@RequestParam (value = "type", defaultValue = "reg") String type,
//                            @RequestParam (value = "username") String username,
//                            @RequestParam (value = "checkExist", defaultValue = "true") boolean checkExist,
//                            HttpServletResponse response)
//            throws Exception
//    {
//        if (StringUtils.equalsIgnoreCase("forget", type)) {
//            Customer customer = customerManager.getCustomer(username);
//            if (customer == null) {
//                ResponseUtils.writeErrorResult(response, Constants.ERR_USER_NOT_EXISTED,
//                        "err.bss.customer.not_existed", "customer not existed:" + username);
//                return;
//            }
//        }
//        else {
//            if (checkExist) {
//                Customer customer = customerManager.getCustomer(username);
//                if (customer != null) {
//                    ResponseUtils.writeErrorResult(response, Constants.ERR_USER_EXISTED,
//                            "err.bss.customer.existed", "Customer has already been existed", username);
//                    return;
//                }
//            }
//        }
//
//        try {
//            verifyCodeManager.sendVerifyCode(type, username);
//            ResponseUtils.writeSuccessResult(response);
//        }
//        catch (BssException e) {
//            log.error("Can't send verify code:", e);
//            ResponseUtils.writeErrorResult(response, e);
//        }
//    }
//
//    @RequestMapping (value = "/register.json")
//    public void register(@RequestBody RegisterRequest request, HttpServletResponse response)
//            throws Exception
//    {
//        //check verify code
//        String code = request.getVerifyCode();
//        Customer customer = request.getCustomer();
//        try {
//            boolean result = verifyCodeManager.checkVerifyCode("reg", customer.getName(), request.getVerifyCode());
//            if (!result) {
//                ResponseUtils.writeErrorResult(response, Constants.ERR_INVALID_CODE,
//                        "err.bss.customer.invalid_verify_code",
//                        "invalid verify code " + code, customer.getName(), code);
//                return;
//            }
//        }
//        catch (BssException e) {
//            log.error("Can't check verify code:", e);
//            ResponseUtils.writeErrorResult(response, e);
//            return;
//        }
//
//        CustomerContext context = new CustomerContext(CustomerContext.TYPE_REGISTER_CUSTOMER);
//        context.setCustomer(customer);
//
//        try {
//            provisionService.provision(context);
//            ResponseUtils.writeSuccessResult(response);
//        }
//        catch (BssException e) {
//            log.error("Can't create customer:", e);
//            String errorCode = e.getStatusCode();
//            int err = ResponseUtils.STATUS_ERROR;
//            if (StringUtils.equalsIgnoreCase(errorCode, CustomerConstants.ERR_CUSTOMER_EXISTED)) {
//                err = Constants.ERR_USER_EXISTED;
//            }
//            ResponseUtils.writeErrorResult(response, err, e.getStatusCode(), e.getMessage(), e.getArguments());
//        }
//    }
//
//    @RequiresAuthentication
//    @RequestMapping (value = "/modifyPassword.json")
//    public void modifyPassword(@RequestBody ModifyPasswordRequest request, Subject subject,
//                               HttpServletResponse response)
//            throws Exception
//    {
//        Customer customer = (Customer) subject.getPrincipal();
//        try {
//            customerManager.modifyPassword(customer.getId(), request.getOldPassword(), request.getPassword());
//        }
//        catch (CustomerException e) {
//            log.error("Can't update password:", e);
//            ResponseUtils.writeErrorResult(response, Constants.ERR_AUTH_FAIL, e.getStatusCode(), e.getMessage());
//            return;
//        }
//        ResponseUtils.writeSuccessResult(response);
//    }
//
//    @RequestMapping (value = "/forgetPassword.json")
//    public void forgetPassword(@RequestBody ForgetPasswordRequest request,
//                               HttpServletResponse response)
//            throws Exception
//    {
//        //check verify code
//        String code = request.getVerifyCode();
//        String username = request.getUsername();
//        try {
//            boolean result = verifyCodeManager.checkVerifyCode("forget", username, code);
//            if (!result) {
//                ResponseUtils.writeErrorResult(response, Constants.ERR_INVALID_CODE,
//                        "err.bss.customer.invalid_verify_code",
//                        "invalid verify code " + code, request.getUsername(), code);
//                return;
//            }
//        }
//        catch (BssException e) {
//            ResponseUtils.writeErrorResult(response, e);
//            return;
//        }
//
//        Customer customer = customerManager.getCustomer(username);
//        if (customer == null) {
//            ResponseUtils.writeErrorResult(response, Constants.ERR_USER_NOT_EXISTED,
//                    "err.bss.customer.not_existed", "customer not existed:" + username);
//        }
//        else {
//            customerManager.resetPassword(customer.getId(), request.getPassword());
//            ResponseUtils.writeSuccessResult(response);
//        }
//    }
//
//    @RequiresAuthentication
//    @RequestMapping (value = "/update.json")
//    public void updateInfo(@RequestBody Customer info, HttpServletResponse response)
//            throws Exception
//    {
//        Subject user = SecurityUtils.getSubject();
//        Customer customer = (Customer) user.getPrincipal();
//        customer.setCity(info.getCity());
//        customer.setMobile(info.getMobile());
//        customer.setCountry(info.getCountry());
//        customer.setCounty(info.getCounty());
//        customer.setEmail(info.getEmail());
//        customer.setGender(info.getGender());
//        customer.setNickName(info.getNickName());
//        customer.setProvince(info.getProvince());
//        customer.setTrueName(info.getTrueName());
//        customerManager.updateCustomer(customer);
//        ResponseUtils.writeSuccessResult(response);
//    }
//
////    @RequestMapping (value = "/login.json")
////    public void login(@RequestParam (value = "username") String username,
////                      @RequestParam (value = "password") String password, HttpServletResponse response)
////            throws Exception
////    {
////        Subject user = SecurityUtils.getSubject();
////        if (!user.isAuthenticated()) {
////            CustomerPasswordToken token = new CustomerPasswordToken();
////            token.setUsername(username);
////            token.setPassword(password.toCharArray());
////            user.login(token);
////        }
////
////        if (user.isAuthenticated()) {
////            Customer customer = (Customer) user.getPrincipal();
////            HashMap result = new HashMap();
////            result.put("token", user.getSession().getId());
////            result.put("customer", customer);
////            ResponseUtils.writeSuccessResult(response, result);
////        }
////        else {
////            ResponseUtils.writeErrorResult(response, ApiConstants.ERR_AUTH, "err.zyt.customer.auth_fail",
////                    "invalid username or password");
////        }
////    }
//
//    @RequestMapping (value = "/login.json")
//    public void login(@RequestParam (value = "username") String username,
//                      @RequestParam (value = "password") String password,
//                      HttpServletRequest request, HttpServletResponse response)
//            throws Exception
//    {
//        Subject user = SecurityUtils.getSubject();
//        LoginJournal loginJournal = new LoginJournal();
//        loginJournal.setLoginMode("mobile");
//        loginJournal.setLoginDate(new Date());
//        loginJournal.setCustomerName(username);
//        String ua = request.getHeader("User-Agent");
//        loginJournal.setClientType(UserAgentUtils.getMobileType(ua));
//        loginJournal.setUserAgent(ua);
//        loginJournal.setClientAddress(request.getRemoteAddr());
//
//        if (!user.isAuthenticated()) {
//            CustomerPasswordToken token = new CustomerPasswordToken();
//            token.setUsername(username);
//            token.setPassword(password.toCharArray());
//            user.login(token);
//        }
//
//        if (user.isAuthenticated()) {
//            Customer customer = (Customer) user.getPrincipal();
//            HashMap result = new HashMap();
//            result.put("token", user.getSession().getId());
//            result.put("customer", customer);
//            ResponseUtils.writeSuccessResult(response, result);
//            loginJournal.setCustomerId(customer.getId());
//            loginJournal.setLoginResult(true);
//        }
//        else {
//            ResponseUtils.writeErrorResult(response, Constants.ERR_AUTH_FAIL, "err.bss.customer.auth_fail",
//                    "invalid username or password");
//            loginJournal.setLoginResult(false);
//            loginJournal.setLoginRemark("invalid username or password");
//        }
//        journal.infoEntity(loginJournal);
//    }
}
